/*
 * JDKPropertiesConfFileUtile.java    0.0.1   01/ott/2009
 *
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */ 
package org.mentalsmash.tazio.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.Properties;

/**
 * {@link ConfigutationFileUtil} implementation using {@link java.util.Properties}
 * @version    0.0.1    01/ott/2009    
 * @author     Andrea Reale <andrea.reale@gmail.com>
 *
 */
public class JDKPropertiesConfFileUtil extends ConfigurationFileUtil {


    private final Properties properties;
    
    protected JDKPropertiesConfFileUtil() {
	properties = new Properties();
    }
    
    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.utils.ConfigurationFileUtil#getIntProperty(java.lang.String)
     */
    @Override
    public int getIntProperty(final String key) {
	int parsed = 0;
	try { 
	  parsed = Integer.parseInt(properties.getProperty(key));
	} catch (NumberFormatException e) {
	    log.warn("Unable to parse integer property havin key: {}. Returning the default", key);
	}
	
	return parsed;
    }

    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.utils.ConfigurationFileUtil#getPropertyKeys()
     */
    @SuppressWarnings("unchecked")
    @Override
    public Enumeration<String> getPropertyKeys() {
	return (Enumeration<String>) properties.propertyNames();
    }

    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.utils.ConfigurationFileUtil#getStringProperty(java.lang.String)
     */
    @Override
    public String getStringProperty(final String key) {
	return properties.getProperty(key);
    }

    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.utils.ConfigurationFileUtil#load(java.lang.String)
     */
    @Override
    public void load(final String path) throws IOException {
	File input = new File(path);
	if( ! (input.exists() && input.canRead() ) ) {
	    log.error("File {} does not exists or does not have read permissions", path);
	    throw new IOException("File does not exists or does not have read permissions");
	}
	
	InputStreamReader reader = 
	    new InputStreamReader(new FileInputStream(input), Charset.forName(DEFAULT_ENCODING));
	
	properties.load(reader);
	
    }

    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.utils.ConfigurationFileUtil#setIntProperty(java.lang.String, int)
     */
    @Override
    public void setIntProperty(final String key, final int value) {
	properties.setProperty(key, Integer.toString(value));
	
    }

    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.utils.ConfigurationFileUtil#setStringProperty(java.lang.String, java.lang.String)
     */
    @Override
    public void setStringProperty(final String key, final String value) {
	properties.setProperty(key, value);
	
    }

    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.utils.ConfigurationFileUtil#store(java.lang.String)
     */
    @Override
    public void store(final String path, final String comments) throws IOException {
	File output = new File(path);
	if(output.exists()) {
	    log.debug("File {} exists, overwriting...", path);
	}
	
//	if( ! ( output.canWrite()) ) {
//	    log.error("File {} does  does not have write permissions", path);
//	    throw new IOException("File does not have read permissions");
//	}
	
	OutputStreamWriter osw =
	    new OutputStreamWriter(new FileOutputStream(output), Charset.forName(DEFAULT_ENCODING));
	
	properties.store(osw, comments);
	
	
    }

    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.utils.ConfigurationFileUtil#getBooleanProperty(java.lang.String)
     */
    @Override
    public boolean getBooleanProperty(final String key) {
	String val = properties.getProperty(key);
	
	return Boolean.parseBoolean(val);

    }

    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.utils.ConfigurationFileUtil#setBooleanProperty(java.lang.String, boolean)
     */
    @Override
    public void setBooleanProperty(final String key, final boolean value) {
	properties.setProperty(key, Boolean.toString(value));
	
    }


    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
	return "JDKPropertiesConfFileUtil [properties=" + properties + "]";
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result
		+ ((properties == null) ? 0 : properties.hashCode());
	return result;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
	if (this == obj)
	    return true;
	if (obj == null)
	    return false;
	if (getClass() != obj.getClass())
	    return false;
	JDKPropertiesConfFileUtil other = (JDKPropertiesConfFileUtil) obj;
	if (properties == null) {
	    if (other.properties != null)
		return false;
	} else if (!properties.equals(other.properties))
	    return false;
	return true;
    }
    
    

    

}
